package uk.ac.lkl.lpp.ui;

import java.util.logging.Logger;

import uk.ac.lkl.lpp.model.ldo.*;
import uk.ac.lkl.common.allocator.AllocatorModel;

/**
 * 
 * manage model backward compatibility.
 * <em>refactor</em>: should probably be placed elsewhere.
 * @author yish
 *
 */
public final class BackCompat {
	private BackCompat (){}
	private Logger logger = Logger.getLogger("uk.ac.lkl");
	
	public static final boolean isOldForm(String className){
		return ((className != null) && (className.startsWith("lpp") || className.startsWith("general")));
	}
	
	public static final String upgrade(String className) {
		if (className.startsWith("lpp")) return "uk.ac.lkl."+className;
		if (className.startsWith("general")) return "uk.ac.lkl.common"+className.substring(7);
		return className;
	}
	
	/**
	 * patch an object from an older version.
	 * the idea is to have all the ugly bits in one place.
	 * @param version the version in which the object was created.
	 * @param o the object to patch
	 */
	public static void backPatch(int version, Object o){
		
		if (version < 570) {
			// version 570 introduced selected property in TMs, so pre-570 TMs need to have
			// it set to true
		    
		    // darrenp: no longer done this way
//			if (o.getClass().equals(TeachingMethod.class)) {
//				((TeachingMethod)o).setSelected(true);
//				
//			}
			//<AllocationSet class="uk.ac.lkl.lpp.model.ldo.TeachingMethod" slots="10" label="Week">
			// need to add an AllocationSet for TMs to the AllocationSet 
			if(o.getClass().equals(AllocatorModel.class)){
				AllocatorModel al=(AllocatorModel)o;
				//al.addAllocation(uk.ac.lkl.lpp.model.ldo.TeachingMethod, 2);
			}
		}
	}
}
